<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Services;

use App\Enums\AdminActionTypeEnum;
use App\Exceptions\ApiException;
use App\Format\HonorDisgracesFormat;
use App\Models\HonorDisgrace;

class HonorDisgracesService
{
    public function create(HonorDisgracesFormat $honorDisgracesFormat)
    {
        $honorDisgracesFormat->setPushTime(time());
        HonorDisgrace::query()->create($honorDisgracesFormat->toArrayNotNull());
        return true;
    }

    public function getList(int $page, int $pageSize, string $orderColumn, string $orderType, array $search)
    {
        $builder = HonorDisgrace::query()->where('push_time', '<', time());
        if (isset($search['type'])) {
            $builder->where('type', $search['type']);
        }
        if (isset($search['start_date'], $search['end_date'])) {
            $builder->where('push_time', '>=', strtotime($search['start_date']));
            if (strlen($search['end_date']) > 10) {
                $builder->where('push_time', '<=', strtotime($search['end_date']));
            } else {
                $builder->where('push_time', '<=', strtotime($search['end_date']) + (24 * 3600));
            }
        }
        if (isset($search['start_date']) && ! isset($search['end_date'])) {
            $builder->where('push_time', '>=', strtotime($search['start_date']));
        }
        if (! isset($search['start_date']) && isset($search['end_date'])) {
            if (strlen($search['end_date']) > 10) {
                $builder->where('push_time', '<=', strtotime($search['end_date']));
            } else {
                $builder->where('push_time', '<=', strtotime($search['end_date']) + (24 * 3600));
            }
        }
        if (isset($search['name']) && strlen($search['name']) > 0) {
            $builder->where('name', 'like', '%' . $search['name'] . '%');
        }

        $count = $builder->count();

        if (strlen($orderColumn) > 0 && strlen($orderType) > 0) {
            $builder->orderBy($orderColumn, $orderType);
        }

        $honors = $builder->skip(($page - 1) * $pageSize)->take($pageSize)->get()->toArray();

        return [
            'page' => $page,
            'page_size' => $pageSize,
            'count' => $count,
            'list' => $honors,
        ];
    }

    public function detail(int $id)
    {
        return HonorDisgrace::query()->find($id);
    }

    public function update(HonorDisgracesFormat $honorDisgracesFormat)
    {
        $id = $honorDisgracesFormat->getId();
        if (empty($id)) {
            throw new ApiException('缺少参数', 1);
        }
        $honorDisgracesFormat->setId(null);
        $honor = HonorDisgrace::query()->find($id);
        $honor->update($honorDisgracesFormat->toArrayNotNull());
        adminLog($honor, AdminActionTypeEnum::UPDATE, '修改荣辱榜');
        return true;
    }

    public function delete($id)
    {
        if (empty($id)) {
            throw new ApiException('缺少参数', 1);
        }
        $honor = HonorDisgrace::query()->find($id);
        adminLog($honor, AdminActionTypeEnum::DELETE, '删除荣辱榜');
        $honor->delete();
        return true;
    }
}
